{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Probability Distributions - Discrete"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "%\n",
    "% plain-TeX file\n",
    "%\n",
    "\\ifx\\pdfoutput\\undefined\n",
    "\\input mbboard\n",
    "\\input mathabx\n",
    "\\fi\n",
    "\\input typofrmt\n",
    "\\input typotabl\n",
    "\\useoptions{magstep1,a4,english,preprint}\n",
    "\n",
    "\\title{Probability Distributions}\n",
    "\\author{Anthony Phan, \\today}\n",
    "\\maketitle\n",
    "\n",
    "\\overfullrule=0pt\n",
    "\\def\\description{\\medbreak\\bgroup\n",
    "\t\\def\\item##1{\\medbreak\\hangindent\\parindent\\leavevmode\n",
    "\t\t\\hskip-\\parindent{{\\tt##1}.}\\enspace\\ignorespaces}%\n",
    "\t\\def\\subitem##1{\\smallbreak\\hangindent2\\parindent\\leavevmode\n",
    "\t\t{\\it##1.}\\enspace\\ignorespaces}%\n",
    "\t\\def\\subsubitem##1{\\par\\hangindent3\\parindent\\leavevmode\n",
    "\t\t\\hskip\\parindent{##1.}\\enspace\\ignorespaces}%\n",
    "\t\\let\\itemitem=\\subitem}%\n",
    "\\def\\enddescription{\\egroup\\medbreak}%\n",
    "\\def\\cs#1{\\hbox{\\tt\\string#1}}%\n",
    "\\def\\var#1{\\ifmmode#1\\else$#1$\\fi}% usual variable\n",
    "\\def\\vari#1{\\ifmmode\\hbox{\\it#1}\\else{\\it#1}\\fi}% variable in italic\n",
    "\\def\\vartype#1{\\ifmmode\\hbox{\\tt#1}\\else{\\tt#1}\\fi}% variable type\n",
    "\n",
    "\\section{Continuous Probability distributions\\footnote{{\\it See}\\/ \\cs{probability-distributions.h}}}\n",
    "\n",
    "\\description\n",
    "\n",
    "\\item{lnGamma(\\vartype{double} $x$)} Logarithm of the Eulerian Gamma\n",
    "function, $\\ln(\\Gamma(x))$.\n",
    "\n",
    "\\item{Gamma(\\vartype{double} $x$)} Eulerian Gamma function\n",
    "$$\n",
    "\t\\mathbb R_+^*\\longrightarrow[\\sqrt\n",
    "\t\\pi,+\\infty\\mathclose[,\\qquad x\\longmapsto \\Gamma(x)\n",
    "\t=\\int_0^{+\\infty}t^{x-1}\\,{\\rm e}^{-t}\\,{\\rm d}t.\n",
    "$$\n",
    "Remember that for $n\\in\\mathbb N$, $n!=\\Gamma(n+1)$.\n",
    "\n",
    "\\item{Beta(\\vartype{double} $x$, \\vartype{double} $y$)} Eulerian Beta function\n",
    "$$\n",
    "\t(\\mathbb R_+^*)^2\\longrightarrow\\mathbb R_+^*,\\qquad\n",
    "\tx\\longmapsto {\\rm B}(x, y)=\\int_{0}^{1}u^{x-1}(1-u)^{y-1}\\,{\\rm d}u\n",
    "\t={\\Gamma(x)\\times\\Gamma(y)\\over\\Gamma(x+y)}\\,.\n",
    "$$\n",
    "\n",
    "\n",
    "% Gamma distributions\n",
    "\n",
    "\\item{gammapdf(\\vartype{double} $x$, \\vartype{double} $a$, \\vartype{double} $\\lambda$)} Probability density function of\n",
    "the Gamma distribution with parameter $a>0$, $\\lambda>0$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "\t\\mathbb 1_{\\mathbb R_+}(x){\\lambda^a\\over\\Gamma(a)}\\, x^{a-1}\\,{\\rm\n",
    "\te}^{-\\lambda x}.\n",
    "$$\n",
    "\n",
    "\\item{gammacdf(\\vartype{double} $x$, \\vartype{double} $a$, \\vartype{double} $\\lambda$)} Cumulative distribution function\n",
    "of the Gamma distribution with parameter $a>0$, $\\lambda>0$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1\\mathclose[,\\qquad\n",
    "\tx\\longmapsto\\mathbb 1_{\\mathbb R_+}(x){\\lambda^a\\over\\Gamma(a)}\n",
    "\t\\int_0^x t^{a-1}\\,{\\rm e}^{-\\lambda t}\\,{\\rm d}t.\n",
    "$$\n",
    "\n",
    "\\item{gammaicdf(\\vartype{double} $p$, \\vartype{double} $a$, \\vartype{double} $\\lambda$)} Inverse cumulative\n",
    "distribution function of the Gamma distribution with parameter $a>0$, $\\lambda>0$.\n",
    "$$\n",
    "\t[0,1\\mathclose[\\longrightarrow\\mathbb R_+,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm gammaicdf}(p,a,\\lambda).\n",
    "$$\n",
    "It is set to $0$ for $p<\\cs{accuracy}$ and to \\cs{infinity} for\n",
    "$p>1-\\cs{accuracy}$.\n",
    "\n",
    "% N(0,1) distribution\n",
    "\n",
    "\\item{normlimit} Numerical parameter for normal computations: if $X$ is a\n",
    "random variable with law $\\mathcal N(0,1)$, the normal distribution\n",
    "with mean $0$ and standard deviation $1$, then $\\mathbb\n",
    "P\\{X\\geq\\cs{normlimit}\\}=\\mathbb\n",
    "P\\{X\\leq-\\cs{normlimit}\\}\\approx0$. Its value is (unreasonably) set to\n",
    "$\\cs{normlimit} = 10$ ($\\cs{normlimit} = 4$ should be sufficient).\n",
    "\n",
    "\\item{normalpdf(\\vartype{double} $x$)} Probability density function of\n",
    "$\\mathcal N(0,1)$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto{\\rm\n",
    "\te}^{-x^2\\!/2}/\\sqrt{2\\pi}.\n",
    "$$\n",
    "\n",
    "\n",
    "\\item{normalcdf (\\vartype{double} $x$)} Cumulative distribution function of\n",
    "$\\mathcal N(0,1)$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathopen]0,\\mathclose1[,\\qquad\n",
    "\tx\\longmapsto\\Phi(x)=\\int_{-\\infty}^x{\\rm e}^{-z^2\\!/2}\\, {{\\rm\n",
    "\td} z\\over\\sqrt{2\\pi}}\n",
    "\t={1\\over2}+{1\\over\\sqrt{2\\pi}}\\sum_{n=0}^\\infty\n",
    "\t{(-1)^n\\,x^{2n+1}\\over (2n+1)\\times n!}\\,.\n",
    "$$\n",
    "It is computed with its associated power series when\n",
    "$|x|<\\cs{normlimit}$, and set to $0$ or $1$ otherwise.\n",
    "\n",
    "\\item{normalcdf\\_(\\vartype{double} $x$)} Cumulative distribution function\n",
    "of $\\mathcal N(0,1)$.  It is just another implementation of the\n",
    "previous function with a Gamma cumulative distribution function since\n",
    "$$\n",
    "\t\\Phi(x)= {1\\over2}\\,\\bigl(\\mathop{\\rm sgn}(x)\n",
    "\t\\times\\mathop{\\rm gammacdf}(x^2\\!/2,1/2)+1,1\\bigr),\n",
    "\t\\qquad\\hbox{for all $x\\in\\mathbb R$.}\n",
    "$$\n",
    "\n",
    "\\item{normalicdf(\\vartype{double} $p$)} Inverse cumulative\n",
    "distribution function of $\\mathcal N(0,1)$.\n",
    "$$\n",
    "\t\\mathopen]0,1\\mathclose[\\longrightarrow\\mathbb R,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm\n",
    "\tnormalicdf}(p)=\\Phi^{-1}(p).\n",
    "$$\n",
    "It is set to $\\pm\\cs{infinity}$ for $p$ outside of\n",
    "$\\mathopen]\\cs{accuracy},1-\\cs{accuracy}\\mathclose[$.\n",
    "Of course, there is also \\cs{normalicdf\\_}\\dots\n",
    "\n",
    "\\remark\n",
    "Normal distributions with mean $m$ and standard deviation $\\sigma$ are not inplemented since they an be easily derived from the standard normal distribution. For instance, one can set\n",
    "\\verbatim\n",
    "double gaussianpdf(double x, double m, double sigma){\n",
    "  return normalpdf((x-m)/sigma)/sigma;}\n",
    "double gaussiancdf(double x, double m, double sigma){\n",
    "  return normalcdf((x-m)/sigma);}\n",
    "double gaussianicdf(double p, double m, double sigma){\n",
    "  return sigma*normalicdf(p)+m;}\n",
    "\\endverbatim\n",
    "in order to get the probability, cumulative, inverse cumulative distribution functions of the $\\mathcal N(m,\\sigma^2)$ distribution with $m\\in\\mathbb R$ and $\\sigma>0$.\n",
    "\\endremark\n",
    "\n",
    "% $\\chi^2$ distributions\n",
    "\n",
    "\\item{chisquarepdf(\\vartype{double} $x$, \\vartype{double} $\\nu$)} Probability density function\n",
    "of $\\chi^2(\\nu)$, the chi-square (Pearson) distribution with $\\nu>0$\n",
    "degrees of freedom.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\\mathbb\n",
    "\t1_{\\mathbb R_+}(x) \\,{x^{\\nu/2-1}{\\rm e}^{-x/2}\\over\n",
    "\t2^{\\nu/2}\\,\\Gamma(\\nu/2)}\\,.\n",
    "$$\n",
    "\n",
    "\\item{chisquarecdf(\\vartype{double} $x$, \\vartype{double} $\\nu$)} Cumulative distribution\n",
    "function of $\\chi^2(\\nu)$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\\mathbb\n",
    "\t1_{\\mathbb R_+}(x) \\int_0^x\\,{t^{\\nu/2-1}{\\rm e}^{-t/2} \\,{{\\rm\n",
    "\td}t\\over 2^{\\nu/2}\\,\\Gamma(\\nu/2)}}\\,.\n",
    "$$\n",
    "\n",
    "\\item{chisquareicdf(\\vartype{double} $p$, \\vartype{double} $\\nu$)} Inverse cumulative\n",
    "distribution function of $\\chi^2(\\nu)$.\n",
    "$$\n",
    "\t[0,1\\mathclose[\\longrightarrow\\mathbb R_+,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm chisquareicdf}(p,\\nu).\n",
    "$$\n",
    "It is set to $0$ for $p<\\cs{accuracy}$ and to \\cs{infinity} for\n",
    "$p>1-\\cs{accuracy}$.\n",
    "\n",
    "% Beta distributions\n",
    "\n",
    "\\item{betapdf(\\vartype{double} $x$, \\vartype{double} $a$, \\vartype{double} $b$)} Probability density function\n",
    "of the Beta distribution with parameters $a>à$ and $b>0$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\\mathbb\n",
    "\t1_{[0,1]}(x)\\,x^{a-1}(1-x)^{b-1}\\!/{\\rm B}(a,b).\n",
    "$$\n",
    "\n",
    "\\item{betacdf(\\vartype{double} $x$, \\vartype{double} $a$, \\vartype{double} $b$)} Cumulative distribution\n",
    "function of the Beta distribution with parameters $a>0$ and $b>0$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "\t\\int_0^x\\mathbb 1_{[0,1]}(u)\\,u^{a-1}(1-u)^{b-1} \\,{{\\rm\n",
    "\td}u\\over {\\rm B}(a,b)}\\,.\n",
    "$$\n",
    "\n",
    "\\item{betaicdf(\\vartype{double} $p$, \\vartype{double} $a$, \\vartype{double} $b$)} Inverse cumulative\n",
    "distribution function of the Beta distribution with parameters $a>0$\n",
    "and $b>0$.\n",
    "$$\n",
    "\t[0,1]\\longrightarrow\\mathbb R_+,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm betaicdf}(p,a,b).\n",
    "$$\n",
    "It is set to $0$ for $p<\\cs{accuracy}$ and to $1$ for\n",
    "$p>1-\\cs{accuracy}$.\n",
    "\n",
    "% Student's (T) distributions\n",
    "\n",
    "\\item{studentpdf(\\vartype{double} $x$, \\vartype{double} $\\nu$)} Probability density function of\n",
    "$\\mathcal T(\\nu)$, the Student distribution with $\\nu>0$ degrees of\n",
    "freedom.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "  \t{1\\over\\sqrt{\\nu}\\,{\\rm B}(\\nu/2,1/2)} \\biggl(1+{x^2\\over\n",
    "  \t\\nu}\\biggr)^{\\!\\!-(\\nu+1)/2} .\n",
    "$$\n",
    "\n",
    "\\item{studentcdf(\\vartype{double} $x$, \\vartype{double} $\\nu$)} Cumulative distribution\n",
    "function of $\\mathcal T(\\nu)$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "  \t\\int_{-\\infty}^x \\biggl(1+{z^2\\over \\nu}\\biggr)^{\\!\\!-(\\nu+1)/2}\n",
    "  \t{{\\rm d}z\\over\\sqrt{\\nu}\\,{\\rm B}(\\nu/2,1/2)} .\n",
    "$$\n",
    "\\item{studenticdf(\\vartype{double} $p$, \\vartype{double} $\\nu$)} Inverse cumulative\n",
    "distribution function of $\\mathcal T(\\nu)$.\n",
    "$$\n",
    "\t\\mathopen]0,1\\mathclose[\\longrightarrow\\mathbb R,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm studenticdf}(p,\\nu).\n",
    "$$\n",
    "It is set to $\\pm\\cs{infinity}$ for $p$ outside of\n",
    "$\\mathopen]\\cs{accuracy},1-\\cs{accuracy}\\mathclose[$.\n",
    "\n",
    "% Fisher's (F) distributions\n",
    "\n",
    "\\item{fisherpdf(\\vartype{double} $x$, \\vartype{double} $\\nu_1$, \\vartype{double} $\\nu_2$)} Probability density\n",
    "function of $\\mathcal F(\\nu_1,\\nu_2)$, the Fisher distribution with\n",
    "$\\nu_1>0$ and $\\nu_2>0$ degrees of freedom ($\\nu_1$ is the numerator degree\n",
    "of freedom, $\\nu_2$ the demominator degree of freedom).\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "\t\\mathbb 1_{\\mathbb R_+}(x)\\,{(\\nu_1/\\nu_2)^{\\nu_1/2}\\,x^{\\nu_1/2-1}\n",
    "\t\\over {\\rm B}(\\nu_1/2,\\nu_2/2)(1+x\\times \\nu_1/\\nu_2)^{(\\nu_1+\\nu_2)/2}}.\n",
    "$$\n",
    "\n",
    "\\item{fishercdf(\\vartype{double} $x$, \\vartype{double} $\\nu_1$, \\vartype{double} $\\nu_2$)} Cumulative distribution\n",
    "function of $\\mathcal F(\\nu_1,\\nu_2)$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow\\mathbb R_+,\\qquad x\\longmapsto\n",
    "\t\\mathbb 1_{\\mathbb R_+}(x)\\int_0^x\n",
    "\t{(\\nu_1/\\nu_2)^{\\nu_1/2}\\,z^{\\nu_1/2-1} \\over {\\rm B}(\\nu_1/2,\\nu_2/2)(1+z\\times\n",
    "\t\\nu_1/\\nu_2)^{(\\nu_1+\\nu_2)/2}} \\,{\\rm d}z.\n",
    "$$\n",
    "\n",
    "\\item{fishericdf(\\vartype{double} $p$, \\vartype{double} $\\nu_1$, \\vartype{double} $\\nu_2$)} Inverse cumulative\n",
    "distribution function of $\\mathcal F(\\nu_1, \\nu_2)$.\n",
    "$$\n",
    "\t[0,1\\mathclose[\\longrightarrow\\mathbb R_+,\\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm fishericdf}(p,\\nu_1,\\nu_2).\n",
    "$$\n",
    "It is set to $0$ for $p<\\cs{accuracy}$ and to \\cs{infinity} for\n",
    "$p>1-\\cs{accuracy}$.\n",
    "\n",
    "\\section{Discrete Probability distributions\\footnote{{\\it See}\\/ \\cs{probability-distributions.h}}}\n",
    "\n",
    "About quantiles, please note that $q_p=k+0.5$ when\n",
    "$F(k)=p$ for $k\\in\\mathbb N$.\n",
    "\n",
    "% Poisson's distributions\n",
    "\n",
    "\\item{poissonpdf(\\vartype{double} $x$, \\vartype{double} $\\lambda$)} Probability distribution\n",
    "function of $\\mathcal P(\\lambda)$, the Poisson distribution with\n",
    "parameter $\\lambda\\geq0$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1],\\qquad x\\longmapsto\\cases{{\\rm\n",
    "\te}^{-\\lambda}\\,\\lambda^x\\!/x!& if $x\\in\\mathbb N$,\\cr 0\n",
    "\t&otherwise.}\n",
    "$$\n",
    "\\item{poissoncdf(\\vartype{double} $x$, \\vartype{double} $\\lambda$)} Cumulative distribution\n",
    "function of $\\mathcal P(\\lambda)$.\n",
    "\n",
    "\\item{poissonicdf(\\vartype{double} $p$, \\vartype{double} $\\lambda$)} Inverse cumulative\n",
    "distribution function of $\\mathcal P(\\lambda)$.\n",
    "\n",
    "% Binomial distributions\n",
    "\n",
    "\\item{binomialpdf(\\vartype{double} $x$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Probability distribution\n",
    "function of $\\mathcal B(n,\\pi)$, the binomial distribution with\n",
    "parameters $n\\in\\mathbb N^*$ and $\\pi\\in[0,1]$.\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1],\\qquad\n",
    "\tx\\longmapsto\\cases{C_n^x\\,\\pi^x(1-\\pi)^{n-x}& if\n",
    "\t$x\\in\\{0,1,\\dots,n\\}$,\\cr 0 &otherwise.}\n",
    "$$\n",
    "\n",
    "\\item{binomialcdf(\\vartype{double} $x$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Cumulative distribution\n",
    "function of $\\mathcal B(n,\\pi)$.\n",
    "\n",
    "\\item{binomialicdf(\\vartype{double} $p$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Inverse cumulative\n",
    "distribution function of $\\mathcal B(n,\\pi)$.\n",
    "\n",
    "% Geometric distributions\n",
    "\n",
    "\\item{geometricpdf(\\vartype{double} $x$, \\vartype{double} $\\pi$)} Probability distribution\n",
    "function of $\\mathcal G(\\pi)$, the geometrical distribution with\n",
    "parameter $\\pi\\in[0,1]$. Describe the law of the first success rank in\n",
    "an infinitely repeated Bernoulli trial with parameter $\\pi\\in[0,1]$.\n",
    "Thus, it is given by\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1], \\qquad x\\longmapsto\n",
    "\t\\cases{\\pi(1-\\pi)^{x-1}& if $x\\in\\{1,2,3,\\dots\\}$,\\cr 0 &\n",
    "\totherwise.}\n",
    "$$\n",
    "\n",
    "\\item{geometriccdf(\\vartype{double} $x$, \\vartype{double} $\\pi$)} Cumulative distribution\n",
    "function of $\\mathcal G(\\pi)$. It returns the sum up to $x$ of the\n",
    "previous probabilities. Thus it is given by\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1], \\qquad x\\longmapsto\n",
    "\t\\cases{1-(1-\\pi)^{\\mathop{\\rm floor}x}& if $x\\geq1$,\\cr 0 &\n",
    "\totherwise.}\n",
    "$$\n",
    "\n",
    "\\item{geometricicdf(\\vartype{double} $p$, \\vartype{double} $\\pi$)} Inverse cumulative\n",
    "distribution function of $\\mathcal G(\\pi)$.\n",
    "$$\n",
    "\t[0,1]\\longrightarrow\\{1,1.5,2,2.5,3,3.5,\\dots\\}, \\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm geometricicdf}(p,\\pi).\n",
    "$$\n",
    "\n",
    "\\item{negativebinomialpdf(\\vartype{double} $x$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Probability\n",
    "distribution function of the negative binomial ditribution with\n",
    "parameters $n\\in\\mathbb N^*$ and $\\pi\\in[0,1]$. Describe the law of the\n",
    "$n$-th success rank, $n\\in\\mathbb N^*$, in an infinitely repeated\n",
    "Bernoulli trial with parameter $\\pi\\in[0,1]$.  Thus, it is given by\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1],\\qquad x\\longmapsto\n",
    "\t\\cases{C_{x-1}^{n-1}\\,\\pi^n(1-\\pi)^{x-n}& if\n",
    "\t$x\\in\\{n,n+1,n+2,\\dots\\}$,\\cr 0 & otherwise.}\n",
    "$$\n",
    "One can remark that the negative binomial distributions for $n=1$ are\n",
    "just the corresponding geometric ones. It is certainly better when\n",
    "$n=1$ to use functions related to geometric distributions instead of\n",
    "negative binomial distributions related ones.\n",
    "\n",
    "\\item{negativebinomialcdf(\\vartype{double} $x$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Cumulative\n",
    "distribution function of the negative binomial ditribution with\n",
    "parameters $n\\in\\mathbb N^*$ and $\\pi\\in[0,1]$. One can easily prove\n",
    "that it is given by\n",
    "$$\n",
    "\t\\mathbb R\\longrightarrow[0,1], \\qquad x\\longmapsto\n",
    "\t1-\\mathop{\\rm binomialcdf}(n-1,\\mathop{\\rm floor} x,\\pi).\n",
    "$$\n",
    "\n",
    "\\item{negativebinomialicdf(\\vartype{double} $p$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Inverse\n",
    "cumulative distribution function of the negative binomial ditribution\n",
    "with parameters $n\\in\\mathbb N^*$ and $\\pi\\in[0,1]$.\n",
    "$$\n",
    "\t[0,1]\\longrightarrow\\{n,n+0.5,n+1,n+1.5,\\dots\\}, \\qquad\n",
    "\tp\\longmapsto\\mathop{\\rm\n",
    "\tnegativebinomialicdf}(p,n,\\pi).\n",
    "$$\n",
    "\n",
    "% Hypergeometric distributions\n",
    "\n",
    "\\item{hypergeometricpdf(\\vartype{double} $x$, \\vartype{int} $N$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Probability\n",
    "distribution function of $\\mathcal H(N, n,\\pi)$, the\n",
    "hypergeometrical distribution with parameters $N\\geq n\\in\\mathbb N^*$\n",
    "and $\\pi\\in[0,1]$.  One should have $N\\pi\\in\\mathbb N$.\n",
    "$$\n",
    "\\eqalign{\n",
    "\t\\mathbb R&\\longrightarrow[0,1],\\cr\n",
    "\tx&\\longmapsto\\cases{\\displaystyle\n",
    "\t{C_{N\\pi}^x\\,C_{N(1-\\pi)}^{n-x}\\over C_N^n}& if\n",
    "\t$x\\in\\{\\max(0,n-N(1-\\pi)),\\dots,\\min(n,N\\pi)\\}$,\\cr 0 &otherwise.}\n",
    "\t}\n",
    "$$\n",
    "\n",
    "\\item{hypergeometriccdf(\\vartype{double} $x$, \\vartype{int} $N$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Cumulative\n",
    "distribution function of $\\mathcal H(N, n,\\pi)$.\n",
    "\n",
    "\\item{hypergeometricicdf(\\vartype{double} $p$, \\vartype{int} $N$, \\vartype{int} $n$, \\vartype{double} $\\pi$)} Inverse\n",
    "cumulative distribution function of $\\mathcal H(N,n,\\pi)$.\n",
    "\n",
    "\\section{Rather specific Probability distributions\\footnote{{\\it See}\\/ \\cs{probability-distributions.h}}}\n",
    "\n",
    "\\item{Kolmogorovcdf(\\vartype{double} $x$, \\vartype{int} $n$)} Cumulative\n",
    "distribution function of Kolmogorov distributions.\n",
    "These are the famous probability distributions involved in\n",
    "Kolmogorov--Smirnov (two-sided) Goodness of Fit tests\n",
    "with statistic\n",
    "$$\n",
    "\tK_n={\\|F-F_n\\|}_\\infty=\\sup_{x\\in\\mathbb R}\\bigl|F(x)-F_n(x)\\bigr|\n",
    "\t=\\max\\nolimits_{i = 1}^n \\bigl(F(X_{(i)})-(i-1)/n\\bigr)\n",
    "\t\\vee\\bigl(i/n-F(X_{(i)})\\bigr).\n",
    "$$\n",
    "Their\n",
    "computation is based on ``Evaluating Kolmogorov's Distribution''\n",
    "by George Marsa\\-glia and Wai Wan Tsang.\n",
    "\n",
    "\\item{kolmogorovicdf(\\vartype{double} $p$, \\vartype{int} $n$)} Inverse\n",
    "cumulative distribution function of Kolmogorov distributions.\n",
    "(Please do not use it since it is based on the bisection\n",
    "method or dichotomy.)\n",
    "\n",
    "\\item{klmcdf(\\vartype{double} $x$, \\vartype{int} $n$)}\n",
    "Cumulative distribution function of the limiting distributions\n",
    "associated to Kolmogorov distribution by Dudley's asymptotic\n",
    "formula (1964):\n",
    "$$\n",
    "\t\\lim_{n\\to\\infty}\\mathbb P\\bigl\\{K_n\\leq u/\\!\\sqrt n\\bigr\\}\n",
    "\t=1+2\\sum_{k=1}^\\infty(-1)^k\\exp\\bigl(-2k^2u^2\\bigr),\n",
    "$$\n",
    "with some numerical adjustments (Stephens M.A., 1970).\n",
    "\n",
    "\\item{klmicdf(\\vartype{double} $p$, \\vartype{int} $n$)} Inverse\n",
    "cumulative distribution function of the previous distributions.\n",
    "\n",
    "\n",
    "\\item{kpmcdf(\\vartype{double} $x$, \\vartype{int} $n$)}\n",
    "Cumulative distribution function of the distribution involved in the\n",
    "one sided  Goodness of Fit tests with statistic\n",
    "$$\n",
    "\tK^+_n=\\sup_{x\\in\\mathbb R}\\bigl(F(x)-F_n(x)\\bigr)\n",
    "\t=\\max\\nolimits_{1\\leq i\\leq n}\n",
    "\t\\Bigl({i\\over n}-F\\bigl(X_{(i)}\\bigr)\\Bigr)\n",
    "$$\n",
    "or\n",
    "$$\n",
    "\tK^-_n=\\sup_{x\\in\\mathbb R}\\bigl(F_n(x)-F(x)\\bigr)\n",
    "\t=\\max\\nolimits_{1\\leq i\\leq n}\n",
    "\t\\Bigl(F\\bigl(X_{(i)}\\bigr)-{i-1\\over n}\\Bigr)\n",
    "$$\n",
    "which share the same distribution: for $x\\in[0,1]$,\n",
    "$$\n",
    "\\eqalign{\n",
    "        \\hbox{\\tt kpmcdf}(x,n)\n",
    "        =\\Proba\\bigl\\{K^\\pm_n\\leq x\\bigr\\}\n",
    "\t&=x\\sum_{0\\leq k\\leq nx}{n\\choose k}(k/n-x)^k(x+1-k/n)^{n-k-1}\\cr\n",
    "        &=1-x\\sum_{nx<k\\leq n}{n\\choose k}(k/n-x)^k(x+1-k/n)^{n-k-1},\\cr\n",
    "}\n",
    "$$\n",
    "the second formula being the one used for computations.\n",
    "\n",
    "\\item{kpmicdf(\\vartype{double} $p$, \\vartype{int} $n$)} Inverse\n",
    "cumulative distribution function of the previous distributions.\n",
    "\n",
    "\\enddescription\n",
    "\n",
    "\\newpage\n",
    "\\section*{Appendix: tables\\footnote{{\\it See}\\/ \\cs{probability-tables.c}}}\n",
    "\n",
    "\\def\\truc#1#2{\\subsection*{#1}#2\\medbreak\n",
    "\\input #1.table\\relax\\breakupto{0.25\\vsize}}\n",
    "\n",
    "\\truc{normalcdf}{This table provides $\\Phi(z)$ for $0\\leq z<3$,\n",
    "where $\\Phi$ is the cumulative distribution function of te standard\n",
    "normal distribution.}\n",
    "\\truc{normalicdf}{This table provides $\\Phi^{-1}(\\alpha)$ for\n",
    "$0.5\\leq\\alpha<1$. Note that $\\Phi^{-1}(1-\\alpha)=-\\Phi^{-1}(\\alpha)$.}\n",
    "\\truc{normalicdfbis}{This table provides $\\Phi^{-1}(1-\\alpha/2)$ for\n",
    "$0<\\alpha<1$.}\n",
    "\\truc{normalicdfter}{This table provides $\\Phi^{-1}(1-\\alpha/2)$ for\n",
    "very small values of $\\alpha$.}\n",
    "\\truc{chisquareicdf}{This table provides $\\cs{chisquareicdf}(1-\\alpha,\\nu)$.}\n",
    "\\truc{studenticdf}{This table provides $\\cs{studenticdf}(1-\\alpha/2,\\nu)$.}\n",
    "\\truc{fishericdf}{This table provides $\\cs{fishericdf}(1-\\alpha,\\nu_1,\\nu_2)$\n",
    "with $\\alpha=0.05$. Values are roughly rounded.}\n",
    "\\truc{kolmogorovicdf}{This table provides $\\cs{kolmogorovicdf}(1-\\alpha,n)$.}\n",
    "\\truc{klmicdf}{This table provides $\\cs{klmicdf}(1-\\alpha,n)$, the usual asymptotic approximation of the Kolmogorov inverse cumulative distribution function.}\n",
    "\n",
    "\\bye"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
